<!doctype html>
<meta charset="utf-8">
<title>Background Sync API: Verifies that the one-shot sync API works
    correctly.</title>
<script src="../resources/permissions-helper.js"></script>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../serviceworker/resources/test-helpers.js"></script>
<script>

promise_test(function(t) {
  const url = '/resources/empty-worker.js';
  const iframe_scope = 'oneshot.html';
  const scope = '/resources/scope/background_sync/' + iframe_scope;
  var sync_manager;

  // This test verifies that one-shot syncs can be registered from an iframe.
  return PermissionsHelper.setPermission('background-sync', 'granted')
    .then(function() {
        return service_worker_unregister_and_register(t, url, scope);
      })
    .then(function(sw_registration_page) {
        return wait_for_state(t, sw_registration_page.installing, 'activated');
      })
    .then(function() {
        return with_iframe(scope)
      })
    .then(function(frame) {
        var w = frame.contentWindow;
        return w.navigator.serviceWorker.getRegistration(iframe_scope);
      })
    .then(function(sw_registration_frame) {
        sync_manager = sw_registration_frame.sync;
        return sync_manager.getTags();
      })
    .then(function(tags) {
        assert_equals(tags.length, 0, 'One-shot syncs should be ' +
                      'cleared at the start of the test.');
        return sync_manager.register('iframe-oneshot');
      })
    .then(function() {
        return service_worker_unregister(t, scope);
      });
  }, 'Background Sync API should allow one-shot syncs to be registered ' +
     'from an iframe');

promise_test(function(t) {
  const url = '/resources/empty-worker.js';
  const scope = '/resources/scope/background_sync/oneshot-uncontrolled.html';
  var sync_manager;

  // This test verifies that one-shot syncs can be registered from uncontrolled
  // documents.
  return PermissionsHelper.setPermission('background-sync', 'granted')
    .then(function() {
        return service_worker_unregister_and_register(t, url, scope);
      })
    .then(function(sw_registration) {
        sync_manager = sw_registration.sync;
        return wait_for_state(t, sw_registration.installing, 'activated');
      })
    .then(function() { return sync_manager.getTags(); })
    .then(function(tags) {
        assert_equals(tags.length, 0, 'One-shot syncs should be ' +
                      'cleared at the start of the test.');
        return sync_manager.register('abcde');
      })
    .then(function() {
        return service_worker_unregister(t, scope);
      })
  }, 'Background Sync API should allow one-shot syncs to be registered ' +
     'from an uncontrolled main-frame document');
</script>
